package cn.kgc.shiro.handlers;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 课工场
 * @date 2024/3/26
 * @description 全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({UnknownAccountException.class, IncorrectCredentialsException.class})
    public Map<String, Object> userLoginHandler(Exception e) {
        e.printStackTrace();
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 110);
        result.put("message", "用户名或密码错误");
        return result;
    }


    //  AuthorizationException   没有权限  403
    @ExceptionHandler(AuthorizationException.class)
    public Map<String, Object> authorizationExceptionHandler(AuthorizationException e) {
        e.printStackTrace();
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 403);
        result.put("message", "没有权限,请联系管理员");
        return result;
    }

}
